package com.mumuso.mumusoitem.controller;

import com.mumuso.mumusoitem.pojo.dArcItemPreErrorMessage;
import com.mumuso.mumusoitem.service.dArcItemPreErrorMessageService;
import com.mumuso.mumusoitem.utils.AjaxResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class itemPreErrorInfoController {

    @Autowired
    private dArcItemPreErrorMessageService dArcItemPreErrorMessageService;


    /**
     * 批量添加错误信息元素
     *
     * @param errorInfoList
     * @return
     */
    @PostMapping("/insertItems")
    public AjaxResult addItemErrorInfo(@RequestBody List<dArcItemPreErrorMessage> errorInfoList) {
        if (ObjectUtils.isEmpty(errorInfoList)) {
            return AjaxResult.error("请检查页面是否标记问题点!!!!!");
        }

        int errorLine = dArcItemPreErrorMessageService.batchInsertErrorMessage(errorInfoList);
        if (errorLine > 0) {
            return AjaxResult.success("问题点已全部添加!!!!!");
        } else {
            return AjaxResult.error("添加失败，请检查参数!!!!!");
        }
    }

    /**
     * 根据商品编号查询该商品下所有的错误信息
     *
     * @param itemId
     * @return
     */
    @RequestMapping("/selectErrorByItemId/{itemId}")
    public AjaxResult selectAllErrorInfoByItemId(@PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return AjaxResult.error("参数未被定义!!!!!!");
        }

        try {
            List<dArcItemPreErrorMessage> errorMessages = dArcItemPreErrorMessageService.selectAllErrorInfoByItemId(itemId);
            if (ObjectUtils.isNotEmpty(errorMessages)) {
                return AjaxResult.success("查询出该商品下所有的错误信息!!!!!", errorMessages);
            } else {
                return AjaxResult.error("未找到与该项关联的错误信息!!!!!", errorMessages);
            }
        } catch (Exception e) {
            return AjaxResult.error("发生错误:" + e.getMessage());
        }
    }


    /**
     * 获取所有数量
     *
     * @param itemId
     * @return
     */
    @RequestMapping("/errorCountById/{itemId}")
    public AjaxResult countAllErrorInfoById(@PathVariable String itemId) {
        return AjaxResult.success(dArcItemPreErrorMessageService.countAllErrorByItemId(itemId));
    }


    /**
     * 根据商品编号删除该商品下所有的错误标记
     *
     * @param itemId
     * @return
     */
    @DeleteMapping("/deleteErrorInfoByItemId/{itemId}")
    public AjaxResult deleteErrorMessageByItemId(@PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return AjaxResult.error("参数未被定义!!!!!!");
        }

        int errorByItemId = dArcItemPreErrorMessageService.deleteAllErrorByItemId(itemId);
        if (errorByItemId > 0) {
            return AjaxResult.success("已将该商品审批表所有标记点取消!!!!");
        } else {
            return AjaxResult.error("取消失败，请检查该审核表是否有标记点!!!!");
        }
    }

    /**
     * 根据商品编号以及序号删除该商品信息
     *
     * @param itemId
     * @param series
     * @return
     */
    @DeleteMapping("/deleteSingleErrorByIdAndSeries")
    public AjaxResult deleteSingleErrorInfoByIdAndSeries(@RequestParam String itemId, @RequestParam String series) {
        if (StringUtils.isBlank(itemId) || StringUtils.isBlank(series)) {
            return AjaxResult.error("未能获取到正确的参数!!!!!!");
        }

        try {
            int isDelete = dArcItemPreErrorMessageService.deleteErrorInfoByItemIdAndSeries(itemId, series);
            if (isDelete > 0) {
                return AjaxResult.success("已删除该商品编号为【" + itemId + "】的序号为[" + series + "]的错误标记");
            } else {
                return AjaxResult.error("删除失败!!!!!");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 通过编号获取错误信息
     *
     * @param itemId
     * @return
     */
    @GetMapping("/selectErrorById/{itemId}")
    public AjaxResult selectErrorInfoByUnionStaffName(@PathVariable String itemId) {
        if (StringUtils.isEmpty(itemId)) {
            return AjaxResult.error("该参数不存在!!!!!");
        }

        List<dArcItemPreErrorMessage> messages = dArcItemPreErrorMessageService.selectErrorInfoByUnionStaffName(itemId);
        dArcItemPreErrorMessage message = new dArcItemPreErrorMessage();

        if (ObjectUtils.isEmpty(messages)) {
            return AjaxResult.error("该商品并提出问题点的人，暂时不发给制单人消息提醒!!!!");
        }


        for (int i = 0; i < messages.size(); i++) {
            if (i == 0) {
                dArcItemPreErrorMessage message1 = messages.get(i);
                message.setDArcStaff(message1.getDArcStaff());
                break;
            }
        }
        return AjaxResult.success(message);
    }

}
